package com.meida.module.bus.provider.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.excel.export.styler.ExcelExportStylerDefaultImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.springmvc.base.BaseController;
import com.meida.module.bus.client.entity.BusReportOutfive;
import com.meida.module.bus.provider.service.BusReportOutfiveService;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 对外验工月报5控制器
 *
 * @author flyme
 * @date 2020-11-23
 */
@RestController
@RequestMapping("/bus/reportOutfive/")
public class BusReportOutfiveController extends BaseController<BusReportOutfiveService, BusReportOutfive>  {

    @ApiOperation(value = "对外验工月报5-分页列表", notes = "对外验工月报5分页列表")
    @GetMapping(value = "page")
    public ResultBody pageList(@RequestParam(required = false) Map params) {
        return bizService.pageList(params);
    }

    @ApiOperation(value = "对外验工月报5-分页列表", notes = "对外验工月报5分页列表")
    @GetMapping(value = "common/page")
    public ResultBody pageList1(@RequestParam(required = false) Map params) {
        return bizService.pageList(params);
    }

    @ApiOperation(value = "对外验工月报5-列表", notes = "对外验工月报5列表")
    @GetMapping(value = "list")
    public ResultBody list(@RequestParam(required = false) Map params) {
        return bizService.listEntityMap(params);
    }

    @ApiOperation(value = "对外验工月报5-添加", notes = "添加对外验工月报5")
    @PostMapping(value = "save")
    public ResultBody save(@RequestParam(required = false) Map params) {
        return bizService.add(params);
    }

    @ApiOperation(value = "对外验工月报5-更新", notes = "更新对外验工月报5")
    @PostMapping(value = "update")
    public ResultBody update(@RequestParam(required = false) Map params) {
        return bizService.edit(params);
    }

    @ApiOperation(value = "对外验工月报5-删除", notes = "删除对外验工月报5")
    @PostMapping(value = "remove")
    public ResultBody remove(@RequestParam(required = false) Map params) {
        return bizService.delete(params);
    }


    @ApiOperation(value = "对外验工月报5-详情", notes = "对外验工月报5详情")
    @GetMapping(value = "get")
    public ResultBody get(@RequestParam(required = false) Map params) {
        return bizService.get(params);
    }
    @ApiOperation(value = "对外月报5-导入数据", notes = "对外月报45导入数据")
    @PostMapping(value = "importReportOutfiveData")
    public ResultBody importReportOutOneData(@RequestParam(required = false) Map params, @RequestParam("file") MultipartFile file) {
        ResultBody resultBody = bizService.importBusReportOutfivedata(file, params);
        return resultBody;
    }

    @ApiOperation(value = "对外月报5-导出数据", notes = "对外月报1-导出数据")
    @PostMapping(value = "exportfiveData")
    public void exportTemplate(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false) Map params) {
        TemplateExportParams templateExportParams=new TemplateExportParams("excel/对外验工月报样式5.xlsx",true);
        templateExportParams.setStyle(ExcelExportStylerDefaultImpl.class);
        Map<String, Object> reportBodyMap =  new HashMap<>();
        List<Map<String, Object>> listMap = new ArrayList<>();
        List<EntityMap> busMaterialList = bizService.getExportData(params);
        for (EntityMap entityMap: busMaterialList) {
            Map<String, Object> objectMap = new HashMap<>();
            objectMap.put("c1",entityMap.get("Field1"));  objectMap.put("c2",entityMap.get("repotItem"));
            objectMap.put("c3",entityMap.get("projectDesc").toString());
            objectMap.put("c4",entityMap.get("itemunit")); objectMap.put("c5",entityMap.getBigDecimal("quantity"));
            objectMap.put("c6",entityMap.getBigDecimal("supplyPrice")); objectMap.put("c7",entityMap.getBigDecimal("installPrice"));
            objectMap.put("c8",entityMap.getBigDecimal("synthesizeAmount"));objectMap.put("c9",entityMap.get("remark"));
            objectMap.put("c10",entityMap.getBigDecimal("accumulativeCount"));objectMap.put("c11",entityMap.getBigDecimal("accumulativeAmount"));
            objectMap.put("c12",entityMap.getBigDecimal("reviewedCount"));objectMap.put("c13",entityMap.getBigDecimal("reviewedAmount"));
            objectMap.put("c14",entityMap.getBigDecimal("reviewedCent"));objectMap.put("c15",entityMap.getBigDecimal("refCount"));
            objectMap.put("c16",entityMap.getBigDecimal("refAmount"));objectMap.put("c17",entityMap.getBigDecimal("refCent"));
            objectMap.put("c18",entityMap.getBigDecimal("approvalCount"));objectMap.put("c19",entityMap.getBigDecimal("approvalTotalPrice"));
            objectMap.put("c20",entityMap.get("reportMonth"));objectMap.put("c21",entityMap.get("batchNumber"));
            listMap.add(objectMap);
        }
        reportBodyMap.put("maplist", listMap);
        Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, reportBodyMap);
        File savefile = new File("D:/excel/");
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        String fileName="对外验工月报样式5.xlsx";
        try {
            response.reset();
            response.setContentType("application/octet-stream;charset=UTF-8");
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            OutputStream fos = response.getOutputStream();
            workbook.write(fos);
            fos.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
